package com.woyowodaows.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/7/6.
 * 匹配是否为手机号
 */

public class RegexpUtils {

    /**
     * 判断邮编
     * @param zipString
     * @return
     */
    public static boolean isZipNO(String zipString) {
        String str = "^[1-9][0-9]{5}$";
        return Pattern.compile(str).matcher(zipString).matches();
    }


        /**
         * 验证手机号码
         * @param mobiles
         * @return
         */
    public static final boolean isMobileNO(String mobiles) {
        String phone = "^1[3,4,5,6,7,8,9]\\d{9}$";
        Pattern p = Pattern
                .compile(phone);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 验证邮箱
     */
    public static boolean isEmail(String email){
        String mail = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        Pattern p = Pattern.compile(mail);
        Matcher m = p.matcher(email);
        return m.matches();

    }

    /**
     *允许汉字和字母
     *
     */
    public static boolean isHZ(String str){
        String regex="^[A-Za-z\\u4e00-\\u9fa5]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.matches();

    }

    /**
     * 允许英文和空格
     */
    public static boolean isEnglish(String str){
        String regex ="^[A-Za-z\\s]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 匹配汉字
     * @param str
     * @return
     */
    public static boolean isChinese(String str){
        String regex =  "[\\u4e00-\\u9fa5]+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        return m.matches();

    }

    /**
     * 手机号码中间4位****显示
     */

    public static String getPhone(String s){
        return s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 身份证号替换，保留前四位和后四位
     *
     * 如果身份证号为空 或者 null ,返回null ；否则，返回替换后的字符串；
     *
     * @param idCard 身份证号
     * @return
     */
    public static String idCardReplaceWithStar(String idCard) {

        if (idCard.isEmpty() || idCard == null) {
            return null;
        } else {
            return replaceAction(idCard, "(?<=\\d{2})\\d(?=\\d{2})");
        }
    }

    private static String replaceAction(String username, String regular) {
        return username.replaceAll(regular, "*");
    }

    /**
     * 验证身份证号是否符合规则
     * @param text 身份证号
     * @return
     */
    public  static boolean isCardReal(String text) {
        String REGEX_ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$)";
        return text.matches(REGEX_ID_CARD);
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId
                .substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }
    /**
     *
     *   银行卡号代******
     *
     *
     * */
    public  static String showBank(String bankCard){
        int hideLength = 12;//替换位数，这里替代中间12位
//        int startIndex = bankCard.length()/2-hideLength/2;
        int startIndex = bankCard.length()/4-hideLength/4;
        String replaceSymbol = "*";//替换符号，这里用“#”为例
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i<bankCard.length();i++){
            char number = bankCard.charAt(i);
            if (i>=startIndex-1&&i<startIndex+hideLength){
                stringBuilder.append(replaceSymbol);
            }else {
                stringBuilder.append(number);
            }
        }
        return stringBuilder.toString();
    }
    /**
     *
     * 方法描述 隐藏银行卡号中间的字符串（使用*号），显示前四后四
     *
     * @param cardNo
     * @return
     *
     * @author yaomy
     * @date 2018年4月3日 上午10:37:00
     */
    public static String hideCardNo(String cardNo) {


        int length = cardNo.length();
        int beforeLength = 4;
        int afterLength = 3;
        //替换字符串，当前使用“*”
        String replaceSymbol = "*";
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++) {
            if(i < beforeLength || i >= (length - afterLength)) {
                sb.append(cardNo.charAt(i));
            } else {
                sb.append(replaceSymbol);
            }
        }

        return sb.toString();
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null
                || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }


    /**
     * 判断同时有字母和数字字符
     *
     * @param text
     * @return
     */
    public  static boolean includeEnglishNumber(String text) {
        String str = ".*[a-zA-Z].*[0-9]|.*[0-9].*[a-zA-Z]";
        Pattern pattern = Pattern.compile(str);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }



    /***
     * 只允许字母和数字
     * @param str
     * @return
     */
    public static boolean stringFilter(String str){
        String regEx  =  "[^a-zA-Z0-9]";
        Pattern p   =   Pattern.compile(regEx);
        Matcher m   =   p.matcher(str);
        return   m.matches();
    }

    private static final int MIN_DELAY_TIME= 3000;  // 两次点击间隔不能少于1000ms
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }


}
